﻿<template>
  <div class="report-designer">
    <el-card>
      <template #header>
        <div class="page-header">
          <h3>鎶ヨ〃璁捐鍣?/h3>
          <div class="header-actions">
            <el-button type="primary" @click="previewReport">
              <el-icon><View /></el-icon>
              棰勮鎶ヨ〃
            </el-button>
            <el-button type="success" @click="saveReport">
              <el-icon><Document /></el-icon>
              淇濆瓨鎶ヨ〃
            </el-button>
            <el-button type="info" @click="exportReport">
              <el-icon><Download /></el-icon>
              瀵煎嚭鎶ヨ〃
            </el-button>
          </div>
        </div>
      </template>

      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="component-panel">
            <template #header>
              <span>缁勪欢闈㈡澘</span>
            </template>
            <div class="component-categories">
              <el-collapse v-model="activeComponents">
                <el-collapse-item title="鍩虹缁勪欢" name="basic">
                  <div class="component-list">
                    <div
                      v-for="component in basicComponents"
                      :key="component.type"
                      class="component-item"
                      draggable="true"
                      @dragstart="handleDragStart($event, component)"
                    >
                      <el-icon><component :is="component.icon" /></el-icon>
                      <span>{{ component.name }}</span>
                    </div>
                  </div>
                </el-collapse-item>
                
                <el-collapse-item title="鍥捐〃缁勪欢" name="chart">
                  <div class="component-list">
                    <div
                      v-for="component in chartComponents"
                      :key="component.type"
                      class="component-item"
                      draggable="true"
                      @dragstart="handleDragStart($event, component)"
                    >
                      <el-icon><component :is="component.icon" /></el-icon>
                      <span>{{ component.name }}</span>
                    </div>
                  </div>
                </el-collapse-item>
                
                <el-collapse-item title="鏁版嵁缁勪欢" name="data">
                  <div class="component-list">
                    <div
                      v-for="component in dataComponents"
                      :key="component.type"
                      class="component-item"
                      draggable="true"
                      @dragstart="handleDragStart($event, component)"
                    >
                      <el-icon><component :is="component.icon" /></el-icon>
                      <span>{{ component.name }}</span>
                    </div>
                  </div>
                </el-collapse-item>
              </el-collapse>
            </div>
          </el-card>
        </el-col>

        <el-col :span="12">
          <el-card class="design-area">
            <template #header>
              <span>璁捐鍖哄煙</span>
            </template>
            <div
              class="design-canvas"
              @drop="handleDrop"
              @dragover="handleDragOver"
              @click="selectComponent(null)"
            >
              <div
                v-for="(component, index) in reportComponents"
                :key="component.id"
                class="design-component"
                :class="{ selected: selectedComponent === index }"
                @click.stop="selectComponent(index)"
                @mousedown="startDrag(index, $event)"
              >
                <component
                  :is="getComponentName(component.type)"
                  :config="component.config"
                  :data="component.data"
                />
                <div class="component-actions">
                  <el-button
                    type="primary"
                    size="small"
                    circle
                    @click.stop="editComponent(index)"
                  >
                    <el-icon><Edit /></el-icon>
                  </el-button>
                  <el-button
                    type="danger"
                    size="small"
                    circle
                    @click.stop="deleteComponent(index)"
                  >
                    <el-icon><Delete /></el-icon>
                  </el-button>
                </div>
              </div>
              <div v-if="reportComponents.length === 0" class="empty-canvas">
                <el-icon size="48" color="#c0c4cc"><Document /></el-icon>
                <p>鎷栨嫿缁勪欢鍒版澶勫紑濮嬭璁℃姤琛?/p>
              </div>
            </div>
          </el-card>
        </el-col>

        <el-col :span="6">
          <el-card class="property-panel">
            <template #header>
              <span>灞炴€ч潰鏉?/span>
            </template>
            <div v-if="selectedComponent !== null && reportComponents[selectedComponent]">
              <el-form
                :model="reportComponents[selectedComponent].config"
                label-width="80px"
                size="small"
              >
                <el-divider content-position="left">閫氱敤灞炴€?/el-divider>
                <el-form-item label="缁勪欢ID">
                  <el-input v-model="reportComponents[selectedComponent].id" disabled />
                </el-form-item>
                <el-form-item label="缁勪欢绫诲瀷">
                  <el-input v-model="reportComponents[selectedComponent].type" disabled />
                </el-form-item>
                <el-form-item label="瀹藉害">
                  <el-input-number
                    v-model="reportComponents[selectedComponent].config.width"
                    :min="100"
                    :max="800"
                    style="width: 100%"
                  />
                </el-form-item>
                <el-form-item label="楂樺害">
                  <el-input-number
                    v-model="reportComponents[selectedComponent].config.height"
                    :min="50"
                    :max="600"
                    style="width: 100%"
                  />
                </el-form-item>
                <el-form-item label="X鍧愭爣">
                  <el-input-number
                    v-model="reportComponents[selectedComponent].config.x"
                    :min="0"
                    style="width: 100%"
                  />
                </el-form-item>
                <el-form-item label="Y鍧愭爣">
                  <el-input-number
                    v-model="reportComponents[selectedComponent].config.y"
                    :min="0"
                    style="width: 100%"
                  />
                </el-form-item>

                <template v-if="reportComponents[selectedComponent].type === 'text'">
                  <el-divider content-position="left">鏂囨湰灞炴€?/el-divider>
                  <el-form-item label="鏂囨湰鍐呭">
                    <el-input
                      v-model="reportComponents[selectedComponent].config.text"
                      type="textarea"
                      :rows="3"
                    />
                  </el-form-item>
                  <el-form-item label="瀛椾綋澶у皬">
                    <el-input-number
                      v-model="reportComponents[selectedComponent].config.fontSize"
                      :min="10"
                      :max="72"
                      style="width: 100%"
                    />
                  </el-form-item>
                  <el-form-item label="瀛椾綋棰滆壊">
                    <el-color-picker v-model="reportComponents[selectedComponent].config.color" />
                  </el-form-item>
                  <el-form-item label="瀵归綈鏂瑰紡">
                    <el-select v-model="reportComponents[selectedComponent].config.align" style="width: 100%">
                      <el-option label="宸﹀榻? value="left" />
                      <el-option label="灞呬腑" value="center" />
                      <el-option label="鍙冲榻? value="right" />
                    </el-select>
                  </el-form-item>
                </template>

                <template v-if="reportComponents[selectedComponent].type === 'table'">
                  <el-divider content-position="left">琛ㄦ牸灞炴€?/el-divider>
                  <el-form-item label="鏁版嵁婧?>
                    <el-select v-model="reportComponents[selectedComponent].config.dataSource" style="width: 100%">
                      <el-option label="閿€鍞暟鎹? value="sales" />
                      <el-option label="閲囪喘鏁版嵁" value="purchase" />
                      <el-option label="搴撳瓨鏁版嵁" value="inventory" />
                    </el-select>
                  </el-form-item>
                  <el-form-item label="鏄剧ず琛屾暟">
                    <el-input-number
                      v-model="reportComponents[selectedComponent].config.pageSize"
                      :min="5"
                      :max="100"
                      style="width: 100%"
                    />
                  </el-form-item>
                  <el-form-item label="鏄剧ず杈规">
                    <el-switch v-model="reportComponents[selectedComponent].config.border" />
                  </el-form-item>
                  <el-form-item label="鏄剧ず鏂戦┈绾?>
                    <el-switch v-model="reportComponents[selectedComponent].config.stripe" />
                  </el-form-item>
                </template>

                <template v-if="reportComponents[selectedComponent].type === 'chart'">
                  <el-divider content-position="left">鍥捐〃灞炴€?/el-divider>
                  <el-form-item label="鍥捐〃绫诲瀷">
                    <el-select v-model="reportComponents[selectedComponent].config.chartType" style="width: 100%">
                      <el-option label="鏌辩姸鍥? value="bar" />
                      <el-option label="鎶樼嚎鍥? value="line" />
                      <el-option label="楗煎浘" value="pie" />
                      <el-option label="鏁ｇ偣鍥? value="scatter" />
                    </el-select>
                  </el-form-item>
                  <el-form-item label="鏁版嵁婧?>
                    <el-select v-model="reportComponents[selectedComponent].config.dataSource" style="width: 100%">
                      <el-option label="閿€鍞暟鎹? value="sales" />
                      <el-option label="閲囪喘鏁版嵁" value="purchase" />
                      <el-option label="搴撳瓨鏁版嵁" value="inventory" />
                    </el-select>
                  </el-form-item>
                  <el-form-item label="鏄剧ず鏍囬">
                    <el-switch v-model="reportComponents[selectedComponent].config.showTitle" />
                  </el-form-item>
                  <el-form-item label="鏄剧ず鍥句緥">
                    <el-switch v-model="reportComponents[selectedComponent].config.showLegend" />
                  </el-form-item>
                </template>
              </el-form>
            </div>
            <div v-else class="no-selection">
              <el-icon size="48" color="#c0c4cc"><Setting /></el-icon>
              <p>璇烽€夋嫨涓€涓粍浠舵潵缂栬緫灞炴€?/p>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-card>

    <el-dialog
      v-model="editDialogVisible"
      title="缂栬緫缁勪欢"
      width="600px"
    >
      <el-form :model="editForm" label-width="100px">
        <el-form-item label="缁勪欢鍚嶇О">
          <el-input v-model="editForm.name" />
        </el-form-item>
        <el-form-item label="鏁版嵁婧?>
          <el-select v-model="editForm.dataSource" style="width: 100%">
            <el-option label="閿€鍞暟鎹? value="sales" />
            <el-option label="閲囪喘鏁版嵁" value="purchase" />
            <el-option label="搴撳瓨鏁版嵁" value="inventory" />
          </el-select>
        </el-form-item>
        <el-form-item label="鏌ヨ鏉′欢">
          <el-input
            v-model="editForm.query"
            type="textarea"
            :rows="4"
            placeholder="璇疯緭鍏ユ煡璇㈡潯浠?
          />
        </el-form-item>
      </el-form>
      
      <template #footer>
        <el-button @click="editDialogVisible = false">鍙栨秷</el-button>
        <el-button type="primary" @click="saveEdit">纭畾</el-button>
      </template>
    </el-dialog>

    <el-dialog
      v-model="previewDialogVisible"
      title="鎶ヨ〃棰勮"
      width="90%"
      :close-on-click-modal="false"
    >
      <div class="preview-content">
        <div
          v-for="component in reportComponents"
          :key="component.id"
          class="preview-component"
          :style="{
            width: component.config.width + 'px',
            height: component.config.height + 'px',
            left: component.config.x + 'px',
            top: component.config.y + 'px'
          }"
        >
          <component
            :is="getComponentName(component.type)"
            :config="component.config"
            :data="component.data"
          />
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 鍝嶅簲寮忔暟鎹?
const activeComponents = ref(['basic'])
const selectedComponent = ref(null)
const editDialogVisible = ref(false)
const previewDialogVisible = ref(false)
const draggedComponent = ref(null)
const editForm = reactive({
  name: '',
  dataSource: '',
  query: ''
})

// 鎶ヨ〃缁勪欢
const reportComponents = ref([])

// 鍩虹缁勪欢
const basicComponents = ref([
  { type: 'text', name: '鏂囨湰', icon: 'Document' },
  { type: 'table', name: '琛ㄦ牸', icon: 'Grid' },
  { type: 'image', name: '鍥剧墖', icon: 'Picture' },
  { type: 'line', name: '鍒嗗壊绾?, icon: 'Minus' }
])

// 鍥捐〃缁勪欢
const chartComponents = ref([
  { type: 'chart', name: '鏌辩姸鍥?, icon: 'DataBoard' },
  { type: 'chart', name: '鎶樼嚎鍥?, icon: 'TrendCharts' },
  { type: 'chart', name: '楗煎浘', icon: 'PieChart' },
  { type: 'chart', name: '鏁ｇ偣鍥?, icon: 'Scatter' }
])

// 鏁版嵁缁勪欢
const dataComponents = ref([
  { type: 'query', name: '鏁版嵁鏌ヨ', icon: 'Search' },
  { type: 'filter', name: '鏁版嵁绛涢€?, icon: 'Filter' },
  { type: 'summary', name: '鏁版嵁姹囨€?, icon: 'DataAnalysis' }
])

// 鏂规硶
const handleDragStart = (event, component) => {
  draggedComponent.value = component
  event.dataTransfer.effectAllowed = 'copy'
}

const handleDragOver = (event) => {
  event.preventDefault()
  event.dataTransfer.dropEffect = 'copy'
}

const handleDrop = (event) => {
  event.preventDefault()
  if (draggedComponent.value) {
    const rect = event.currentTarget.getBoundingClientRect()
    const x = event.clientX - rect.left
    const y = event.clientY - rect.top
    
    const newComponent = {
      id: Date.now().toString(),
      type: draggedComponent.value.type,
      config: {
        x: x - 50,
        y: y - 25,
        width: 200,
        height: 100,
        ...getDefaultConfig(draggedComponent.value.type)
      },
      data: getDefaultData(draggedComponent.value.type)
    }
    
    reportComponents.value.push(newComponent)
    draggedComponent.value = null
  }
}

const getDefaultConfig = (type) => {
  const configs = {
    text: {
      text: '鏂囨湰鍐呭',
      fontSize: 14,
      color: '#333333',
      align: 'left'
    },
    table: {
      dataSource: 'sales',
      pageSize: 10,
      border: true,
      stripe: true
    },
    chart: {
      chartType: 'bar',
      dataSource: 'sales',
      showTitle: true,
      showLegend: true
    },
    image: {
      src: '',
      alt: '鍥剧墖'
    }
  }
  return configs[type] || {}
}

const getDefaultData = (type) => {
  const dataMap = {
    sales: [
      { date: '2024-01', amount: 100000, quantity: 100 },
      { date: '2024-02', amount: 120000, quantity: 120 },
      { date: '2024-03', amount: 150000, quantity: 150 }
    ],
    purchase: [
      { date: '2024-01', amount: 80000, quantity: 80 },
      { date: '2024-02', amount: 90000, quantity: 90 },
      { date: '2024-03', amount: 110000, quantity: 110 }
    ],
    inventory: [
      { product: '浜у搧A', stock: 100, value: 50000 },
      { product: '浜у搧B', stock: 150, value: 75000 },
      { product: '浜у搧C', stock: 80, value: 40000 }
    ]
  }
  
  return dataMap[type] || []
}

const selectComponent = (index) => {
  selectedComponent.value = index
}

const editComponent = (index) => {
  const component = reportComponents.value[index]
  editForm.name = component.config.name || ''
  editForm.dataSource = component.config.dataSource || ''
  editForm.query = component.config.query || ''
  editDialogVisible.value = true
}

const deleteComponent = async (index) => {
  try {
    await ElMessageBox.confirm('纭畾瑕佸垹闄よ缁勪欢鍚楋紵', '鎻愮ず', {
      confirmButtonText: '纭畾',
      cancelButtonText: '鍙栨秷',
      type: 'warning'
    })
    
    reportComponents.value.splice(index, 1)
    if (selectedComponent.value === index) {
      selectedComponent.value = null
    } else if (selectedComponent.value > index) {
      selectedComponent.value--
    }
    
    ElMessage.success('缁勪欢鍒犻櫎鎴愬姛')
  } catch {
    // 鐢ㄦ埛鍙栨秷鍒犻櫎
  }
}

const startDrag = (index, event) => {
  // 瀹炵幇缁勪欢鎷栨嫿绉诲姩
  // 杩欓噷鍙互娣诲姞鎷栨嫿绉诲姩閫昏緫
}

const getComponentName = (type) => {
  const componentMap = {
    text: 'ReportText',
    table: 'ReportTable',
    chart: 'ReportChart',
    image: 'ReportImage'
  }
  return componentMap[type] || 'ReportText'
}

const saveEdit = () => {
  if (selectedComponent.value !== null) {
    const component = reportComponents.value[selectedComponent.value]
    component.config.name = editForm.name
    component.config.dataSource = editForm.dataSource
    component.config.query = editForm.query
    component.data = getDefaultData(editForm.dataSource)
  }
  editDialogVisible.value = false
  ElMessage.success('缁勪欢閰嶇疆宸蹭繚瀛?)
}

const previewReport = () => {
  if (reportComponents.value.length === 0) {
    ElMessage.warning('璇峰厛娣诲姞缁勪欢鍒拌璁″尯鍩?)
    return
  }
  previewDialogVisible.value = true
}

const saveReport = () => {
  if (reportComponents.value.length === 0) {
    ElMessage.warning('璇峰厛璁捐鎶ヨ〃鍐呭')
    return
  }
  ElMessage.success('鎶ヨ〃淇濆瓨鎴愬姛')
}

const exportReport = () => {
  if (reportComponents.value.length === 0) {
    ElMessage.warning('璇峰厛璁捐鎶ヨ〃鍐呭')
    return
  }
  ElMessage.success('鎶ヨ〃瀵煎嚭鎴愬姛')
}

// 鐢熷懡鍛ㄦ湡
onMounted(() => {
  // 鍒濆鍖栨暟鎹?
})
</script>

<style scoped>
.report-designer {
  padding: 0;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.page-header h3 {
  margin: 0;
  color: #303133;
}

.header-actions {
  display: flex;
  gap: 12px;
  align-items: center;
}

.component-panel {
  height: 600px;
}

.component-panel .el-card__body {
  height: calc(100% - 40px);
  overflow-y: auto;
}

.component-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.component-item {
  display: flex;
  align-items: center;
  padding: 8px;
  background: #f5f7fa;
  border-radius: 4px;
  cursor: move;
  transition: all 0.3s;
}

.component-item:hover {
  background: #e4e7ed;
}

.component-item .el-icon {
  margin-right: 8px;
  color: #409EFF;
}

.design-area {
  height: 600px;
}

.design-area .el-card__body {
  height: calc(100% - 40px);
  padding: 0;
}

.design-canvas {
  position: relative;
  width: 100%;
  height: 100%;
  background: #fafafa;
  border: 1px dashed #dcdfe6;
  overflow: auto;
}

.design-component {
  position: absolute;
  border: 2px solid transparent;
  cursor: move;
  transition: all 0.3s;
}

.design-component:hover {
  border-color: #409EFF;
}

.design-component.selected {
  border-color: #409EFF;
  box-shadow: 0 0 10px rgba(64, 158, 255, 0.3);
}

.component-actions {
  position: absolute;
  top: -30px;
  right: 0;
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.3s;
}

.design-component:hover .component-actions {
  opacity: 1;
}

.empty-canvas {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: #c0c4cc;
}

.empty-canvas p {
  margin-top: 16px;
  font-size: 14px;
}

.property-panel {
  height: 600px;
}

.property-panel .el-card__body {
  height: calc(100% - 40px);
  overflow-y: auto;
}

.no-selection {
  text-align: center;
  padding: 40px 0;
  color: #c0c4cc;
}

.no-selection p {
  margin-top: 16px;
  font-size: 14px;
}

.preview-content {
  position: relative;
  width: 100%;
  height: 600px;
  background: white;
  border: 1px solid #dcdfe6;
  overflow: auto;
}

.preview-component {
  position: absolute;
  border: 1px solid #dcdfe6;
}
</style>
